package com.aigis.big4.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 参考
 * https://blog.csdn.net/devtao/article/details/16922797
 * https://blog.csdn.net/WYpersist/article/details/79783872
 */
public class SelfJoin {
    public static int time = 0;

    public static class SelfJoinMapper extends Mapper<Object, Text, Text, Text> {
        private Text keyOut = new Text();
        private Text valueOut = new Text();

        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();

            if (!line.contains("child")) {
                String[] childParent = line.split(" ");
                String relation = "1";
                // "1" 代表输出 parent 作为key，child 做value
                keyOut.set(childParent[1]);
                valueOut.set(relation + childParent[0]);
                context.write(keyOut, valueOut);
                // "2" 代表输出 child 作为key， parent 做value
                relation = "2";
                keyOut.set(childParent[0]);
                valueOut.set(relation + childParent[1]);
                context.write(keyOut, valueOut);
            }
        }
    }

    public static class SelfJoinReduce extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            if (time == 0) {
                context.write(new Text("child"), new Text("grandParent"));
                time++;
            }

            List<String> childs = new ArrayList<String>();
            List<String> grandParents = new ArrayList<String>();
            // 数据分组

            for (Text value : values) {
                String info = value.toString();
                String name = info.substring(1);
                if (info.charAt(0) == '1') {
                    childs.add(name);
                } else if (info.charAt(0) == '2') {
                    grandParents.add(name);
                } else {
                    continue;
                }
            }
            //笛卡尔积
            for (int i = 0; i < childs.size(); i++) {
                for (int j = 0; j < grandParents.size(); j++) {
                    String child = childs.get(i);
                    String grandParent = grandParents.get(j);
                    System.out.println("数据结构child:" + child + " grandParent:" + grandParent);
                    context.write(new Text(child), new Text(grandParent));
                }
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("mapred.job.tracker", "hdfs://127.0.0.1:9001");
        configuration.set("fs.default.name", "hdfs://127.0.0.1:9000");

        Job job = Job.getInstance(configuration, "join test");
        job.setJarByClass(SelfJoin.class);
        job.setMapperClass(SelfJoin.SelfJoinMapper.class);
        //加上combineClass 输出结果为空，出错不是所有任务都适合combine

        //job.setCombinerClass(SelfJoin.SelfJoinReduce.class);
        job.setReducerClass(SelfJoin.SelfJoinReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/test/data.txt"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/test/selfJoin"));
        System.out.println(job.waitForCompletion(true) ? 0 : 1);
    }
}
